from django.db import models
from django.contrib.auth.models import User
from readstat.models import ReadNumMethod
from django.urls import reverse
# from django.conf import settings
# 3th part models
from ckeditor_uploader.fields import RichTextUploadingField

# Create your models here.


class BlogType(models.Model):
    type_name = models.CharField(max_length=15)

    def __str__(self):
        return '%s' % self.type_name


class Blog(models.Model, ReadNumMethod):
    title = models.CharField(max_length=50)
    blog_type = models.ForeignKey(BlogType, on_delete=models.CASCADE)
    content = RichTextUploadingField()
    author = models.ForeignKey(
        User, on_delete=models.CASCADE)
    c_time = models.DateTimeField(auto_now_add=True)
    m_time = models.DateTimeField(auto_now=True)

    def __str__(self):
        return 'title: %s' % self.title

    def get_url(self):
        return reverse('blog_detail', kwargs={'blog_pk': self.pk})

    def get_email(self):
        return self.author.email

    class Meta:
        ordering = ['-c_time']
